package com.flying.interview;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.beans.MethodDescriptor;
import java.beans.beancontext.BeanContextServicesListener;
import java.util.*;

/**
 * @ClassName TestMap
 * @Author flying
 * @Date 2025/2/5 16:06
 * @Description
 */
public class TestMap {
    Scanner scanner = new Scanner(System.in);
    /**
     * 添加你喜欢的歌手以及你喜欢他唱过的歌曲,并遍历
     */
    @Test
    void test1() {
        HashMap<String, ArrayList<String>> sings = new HashMap<>();

        String singer = "周杰伦";

        ArrayList<String> singerSongs = new ArrayList<>();
        singerSongs.add("双节棍");
        singerSongs.add("本草纲目");
        singerSongs.add("夜曲");
        singerSongs.add("稻香");


        String singer1 = "陈奕迅";
        ArrayList<String> singer1Songs = new ArrayList<>();
        singer1Songs.add("浮夸");
        singer1Songs.add("十年");
        singer1Songs.add("红玫瑰");
        singer1Songs.add("好久不见");
        singer1Songs.add("孤勇者");

        sings.put(singer, singerSongs);
        sings.put(singer1, singer1Songs);

        // 使用迭代器遍历

        Set<Map.Entry<String, ArrayList<String>>> entries = sings.entrySet();


        Iterator<Map.Entry<String, ArrayList<String>>> iterator = entries.iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, ArrayList<String>> next = iterator.next();
            String key = next.getKey();
            ArrayList<String> value = next.getValue();
            System.out.println("歌手： " + key);
            System.out.println("歌曲： " + value);
        }


    }

    static class CityMap {

        public static Map model = new HashMap();

        static {
            model.put("北京", new String[]{"北京"});
            model.put("辽宁", new String[]{"沈阳", "盘锦", "铁岭", "丹东", "大连", "锦州", "营口"});
            model.put("吉林", new String[]{"长春", "延边", "吉林", "白山", "白城", "四平", "松原"});
            model.put("河北", new String[]{"承德", "沧州", "邯郸", "邢台", "唐山", "保定", "石家庄"});
            model.put("河南", new String[]{"郑州", "许昌", "开封", "洛阳", "商丘", "南阳", "新乡"});
            model.put("山东", new String[]{"济南", "青岛", "日照", "临沂", "泰安", "聊城", "德州"});
        }

    }


    /**
     * 案例：二级联动
     * <p>
     * 将省份和城市的名称保存在集合中，当用户选择省份以后，二级联动，显示对应省份的地级市供用户选择。
     * <p>
     * 效果演示见课件。
     * <p>
     * 提示：如果输入的省份不正确，需要重新输入。 如果输入的城市不正确，需要重新输入。
     */
    @Test
    void test2() {

        Map model = CityMap.model;
        Set provinces = model.keySet();

        for (Object province : provinces) {
            System.out.print(province + "\t");
        }
        System.out.println();
        System.out.println("请选择你所在的省份: ");
        String pr = scanner.next();
        if (!provinces.contains(pr))
            System.out.println("不包含选择的的省份");
        else {
            String[] cities = (String[]) model.get(pr);
            for (String city : cities) {
                System.out.println(city + "\t");
            }
            System.out.println();
            System.out.println("请选择你所在的城市: ");
            String city = scanner.next();
            if (Arrays.binarySearch(cities, city) < 0)
                System.out.println("不包含选择的的城市");
            else {
                System.out.println("信息登记成功");
            }


        }

    }
}
